﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using QualityRpt.Utils;
using System.Data.Odbc;
using Org.SqlHelper;

namespace QualityRpt.Dao.Db2
{
    class SysLogsDao:ISysLogsDao
    {
        const string cachePrefix = "SLOG-";
        public DataRow Save(DataRow row)
        {
            object rt = null;
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                rt = ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "INSERT INTO RPT.SYS_LOGS(LOG_TYPE,LOG_TIME,LOG_DESCRIPTION,LOG_SOURCE,LOG_CATE,LOG_USER,DATA_STATUS) VALUES(?,?,?,?,?,?,?)",
                    ODBCHelper.CreateInputParameter("LOG_TYPE", OdbcType.SmallInt, row["LOG_TYPE"]),
                    ODBCHelper.CreateInputParameter("LOG_TIME", OdbcType.DateTime, row["LOG_TIME"]),
                    ODBCHelper.CreateInputParameter("LOG_DESCRIPTION", OdbcType.VarChar, 255, row["LOG_DESCRIPTION"]),
                    ODBCHelper.CreateInputParameter("LOG_SOURCE", OdbcType.SmallInt, row["LOG_SOURCE"]),
                    ODBCHelper.CreateInputParameter("LOG_CATE", OdbcType.Int, row["LOG_CATE"]),
                    ODBCHelper.CreateInputParameter("LOG_USER", OdbcType.Int, row["LOG_USER"]),
                    ODBCHelper.CreateInputParameter("DATA_STATUS", OdbcType.SmallInt, row["DATA_STATUS"]));
                rt = ODBCHelper.ExecuteScalar(conn, CommandType.Text, "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1");
            }
            CacheManager.RemoveByPrefix(cachePrefix);
            if (rt != null)
            {
                row["ID"] = Convert.ToInt64(rt);
                return row;
            }
            return null;
        }

        public DataRow Update(DataRow row)
        {
            object rt = null;
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                rt = ODBCHelper.ExecuteScalar(conn, CommandType.Text, "UPDATE RPT.SYS_LOGS SET LOG_TYPE=?,LOG_TIME=?,LOG_DESCRIPTION=?,LOG_SOURCE=?,LOG_CATE=?,LOG_USER=?,DATA_STATUS=? WHERE ID=?",
                    ODBCHelper.CreateInputParameter("LOG_TYPE", OdbcType.SmallInt, row["LOG_TYPE"]),
                    ODBCHelper.CreateInputParameter("LOG_TIME", OdbcType.DateTime, row["LOG_TIME"]),
                    ODBCHelper.CreateInputParameter("LOG_DESCRIPTION", OdbcType.VarChar, 255, row["LOG_DESCRIPTION"]),
                    ODBCHelper.CreateInputParameter("LOG_SOURCE", OdbcType.SmallInt, row["LOG_SOURCE"]),
                    ODBCHelper.CreateInputParameter("LOG_CATE", OdbcType.Int, row["LOG_CATE"]),
                    ODBCHelper.CreateInputParameter("LOG_USER", OdbcType.Int, row["LOG_USER"]),
                    ODBCHelper.CreateInputParameter("DATA_STATUS", OdbcType.SmallInt, row["DATA_STATUS"]),
                    ODBCHelper.CreateInputParameter("ID", OdbcType.Int, row["ID"]));
            }
            CacheManager.RemoveByPrefix(cachePrefix);
            return row;
        }

        public DataRow Get(long id)
        {
            DataTable table = GetList("ID=" + id.ToString());
            if (table.Rows.Count > 0)
            {
                return table.Rows[0];
            }
            return null;
        }

        public void Delete(long id)
        {
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "DELETE FROM RPT.SYS_LOGS WHERE ID=?",
                    ODBCHelper.CreateInputParameter("ID", OdbcType.BigInt, id));
            }
            CacheManager.RemoveByPrefix(cachePrefix);
        }

        public DataTable GetList(string where)
        {
            return GetList(where, "ID DESC");
        }

        public DataTable GetList(string where, string sort)
        {
            DataTable rtTable = null;
            StringBuilder sbText = new StringBuilder();
            sbText.Append("SELECT * FROM RPT.SYS_LOGS ");
            if (where.Trim() != string.Empty)
            {
                sbText.Append(" WHERE " + where);
            }
            if (sort.Trim() != string.Empty)
            {
                sbText.Append(" ORDER BY " + sort);
            }
            string cacheKey = cachePrefix + sbText.ToString();
            if (CacheManager.Get(cacheKey) == null)
            {
                using (OdbcConnection conn = ODBCHelper.GetConnection())
                {
                    rtTable = ODBCHelper.ReadTable(conn, CommandType.Text, sbText.ToString());
                }
                CacheManager.Put(cacheKey, rtTable,2);
            }
            else
            {
                rtTable = CacheManager.Get(cacheKey) as DataTable;
            }
            return rtTable;
        }

        public DataTable GetPageList(PageUtils pu, string where, string sort)
        {
            pu.TotalCountSql = "select count(1) from rpt.sys_logs as sl where 1=1 and " + where;
            pu.ListSql = "select sl.* from rpt.sys_logs as sl where 1=1 and " + where + " ORDER BY " + sort;
            return pu.GetDb2OdbcPageList();
        }
    }
}
